package ez430;


/**
 *
 * @author georg
 */
public class Main
{
    public static void main ( String[] args )
    {
        try
        {
            System.setProperty("gnu.io.rxtx.SerialPorts", "/dev/ttyACM0");

            Port port = new Port("/dev/ttyACM0");
            Watch watch = new Watch(port);
            Message msg;

            
            msg = new Message(Message.CMD_RESET, new byte[1]);
            watch.SendAndReceive(msg, 1000);

            msg = new Message(Message.CMD_START_SIMPLICITI, null);
            watch.SendAndReceive(msg, 2000);

            //msg = new Message(Message.CMD_GET_PRODUCT_ID, new byte[4]);
            //watch.SendAndReceive(msg, 100);

            for(int i=0; i < 100; i++){
                msg = new Message(Message.CMD_GET_STATUS, new byte[1]);
                watch.SendAndReceive(msg, 10);

                msg = new Message(Message.CMD_GET_SIMPLICITIDATA, new byte[4]);
                byte[] packet = watch.SendAndReceive(msg, 20);

                System.out.println(packet[4] + " " + packet[5] + " " + packet[6]);
                /*
                int x = packet[4];
                byte sign_x = (byte) ((x & 0x80) >> 7);
                if(sign_x == 1)
                    x = ((((~x) & 0xFF) + 1) * (-1));
                
                System.out.println(packet[4] + " " + x);
                x = packet[5];
                sign_x = (byte) ((x & 0x80) >> 7);
                if(sign_x == 1)
                    x =  ((((~x) & 0xFF) + 1) * (-1));
                System.out.println(packet[5] + " " + x);
                x = packet[6];
                sign_x = (byte) ((x & 0x80) >> 7);
                if(sign_x == 1)
                x = ((((~x) & 0xFF) + 1) * (-1));
                System.out.println(packet[6] + " " +x);
                */
            }

            byte[] data = { 07, 00, 00, 00, 00, 00, 00, 00,
                            00, 00, 00, 00, 00 ,00, 00, 00,
                            00, 00, 00 };


            msg = new Message(Message.CMD_SYNC_SEND_COMMAND, data);
            watch.SendAndReceive(msg, 1000);

            msg = new Message(Message.CMD_STOP_SIMPLICITI, null);
            watch.SendAndReceive(msg, 1000);
            port.close();
        }
        catch ( Exception e )
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}



